﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.IO;

namespace BluEtch.GotPhone.Data
{
    public class IsolatedFileStorage
    {
        /// <summary>
        /// method for creating a directory within isolated storage
        /// </summary>
        /// <param name="directoryName">Name of a directory to be created</param>
        public static void CreateDirectory(string directoryName)
        {
            try
            {
                using (IsolatedStorageFile currentIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (!string.IsNullOrEmpty(directoryName) && !currentIsolatedStorage.DirectoryExists(directoryName))
                    {
                        currentIsolatedStorage.CreateDirectory(directoryName);
                    }
                }
            }
            catch (Exception ex)
            {
                // do something with exception
            }
        }

        /// <summary>
        /// Method for deleting an isolated storage directory
        /// </summary>
        /// <param name="directoryName">Name of a directory to be deleted</param>
        public static void DeleteDirectory(string directoryName)
        {
            try
            {
                using (IsolatedStorageFile currentIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (!string.IsNullOrEmpty(directoryName) && currentIsolatedStorage.DirectoryExists(directoryName))
                    {
                        currentIsolatedStorage.DeleteDirectory(directoryName);
                    }
                }
            }
            catch (Exception ex)
            {
                // do something with exception
            }
        }

        /// <summary>
        /// Method for creating a file in isolated storage
        /// </summary>
        /// <param name="directoryName">Path to directory</param>
        /// <param name="fileNameWithExtention">File name with extention</param>
        /// <param name="content">Content for a file</param>
        /// <param name="createNew">Indicates if a previous version of a file should be deleted before the creation</param>
        public static void CreateFile(string directoryName, string fileNameWithExtention, string content, bool createNew)
        {
            try
            {
                using (IsolatedStorageFile currentIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    // if file name was not specified then do not create a file
                    if (string.IsNullOrEmpty(fileNameWithExtention))
                        return;

                    string filePath = GetFilePath(directoryName, fileNameWithExtention);

                    if (createNew)
                    {
                        if (currentIsolatedStorage.FileExists(filePath))
                        {
                            // if file exists - delete it before creating a new one
                            currentIsolatedStorage.DeleteFile(filePath);
                        }
                    }

                    // open writer stream to write a content to a file
                    StreamWriter destinationFile = new StreamWriter(new IsolatedStorageFileStream(filePath, FileMode.OpenOrCreate, currentIsolatedStorage));

                    // write content to a file in isolated storage
                    destinationFile.WriteLine(content);

                    // close writer stream
                    destinationFile.Close();
                }
            }
            catch (Exception ex)
            {
                // do something with exception
            }
        }

        public static string ReadFile(string directoryName, string fileNameWithExtention)
        {
            string content = null;

            try
            {
                using (IsolatedStorageFile currentIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (!string.IsNullOrEmpty(fileNameWithExtention))
                    {
                        // open a reader stream to read a file content for isolated storage
                        StreamReader fileToRead = new StreamReader(new IsolatedStorageFileStream(
                            GetFilePath(directoryName, fileNameWithExtention)
                            , FileMode.Open
                            , currentIsolatedStorage));

                        content = fileToRead.ReadLine();

                        // close reader stream
                        fileToRead.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                // do something with exception
            }

            return content;
        }

        /// <summary>
        /// Method for deleting a single file from Isolated Storage
        /// </summary>
        /// <param name="directoryName">Directory path for a file</param>
        /// <param name="fileNameWithExtention">File name with extention</param>
        public static void DeleteFile(string directoryName, string fileNameWithExtention)
        {
            try
            {
                using (var currentIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (!string.IsNullOrEmpty(fileNameWithExtention))
                    {
                        currentIsolatedStorage.DeleteFile(GetFilePath(directoryName, fileNameWithExtention));
                    }
                }
            }
            catch (Exception ex)
            {
                // do something with exception
            }
        }

        public static void ClearAllStoredData()
        {
            try
            {
                using (var currentIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    currentIsolatedStorage.Remove();
                }
            }
            catch (Exception ex)
            {
                // do something
            }
        }

        /// <summary>
        /// Utility method that returns a file path based on directory name and input file name parameter
        /// </summary>
        public static string GetFilePath(string directoryName, string fileNameWithExtention)
        {
            if (directoryName != null && !directoryName.EndsWith("\\"))
                directoryName += "\\";

            return (directoryName + fileNameWithExtention);
        }
    }
}
